﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace Utility3D
{
    public class NotifiableCollection<TItem> : ICollection<TItem>
    {
        public Action<TItem> AddHandler { get; set; }

        private readonly List<TItem> _collection = new List<TItem>();

        public IEnumerator<TItem> GetEnumerator()
        {
            return _collection.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public void Add(TItem item)
        {
            if (AddHandler != null)
                AddHandler(item);

            _collection.Add(item);
        }

        public void Clear()
        {
            _collection.Clear();
        }

        public bool Contains(TItem item)
        {
            return _collection.Contains(item);
        }

        public void CopyTo(TItem[] array, int arrayIndex)
        {
            _collection.CopyTo(array, arrayIndex);
        }

        public bool Remove(TItem item)
        {
            return _collection.Remove(item);
        }

        public int Count
        {
            get { return _collection.Count; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }
    }
}